import Vue from "vue";
import Vuex from 'vuex';
Vue.use(Vuex);
const state = {
    count:0
};
// 需求是做一个加法按钮，加传入的值
// 复习写成固定值的
const mutations = {
    add(state,num){
       console.log(state);
       state.count += num; 
       console.log(num);
    },
    /* addObj(state,obj){
        state.count += obj.num;
        console.log(obj);
    } */
    addObj(state,{num}){
        state.count += num;
        console.log(num);
        // console.log(obj);
    },
    decrement(state,num){
        state.count -= num;
        console.log(num);
    },
    decrementObj(state,num){
        state.count -= num;
    }
};
// 需求：count如果是奇数  加   
// 如果是偶数    减
const actions = {
   decrementAct({commit,state},num){
    //    state.count
       console.log(state);
       if(state.count % 2 == 0){
        commit('decrement',num);
        console.log(num);
       } 
     
   },
   decrementObjAct({commit},{num}){
       commit("decrementObj",num);
       console.log(num);
   },
   addNumAct({commit,state},num){
       if(state.count % 2 == 1){
           commit('add',num)
       }
   }
};
const getters = {};
export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters
});